package controllers

import config.MyRow
import dao._
import javax.inject.Inject
import play.api.libs.json.Json
import play.api.mvc.{AbstractController, Action, AnyContent, ControllerComponents}
import utils.TableUtils

import scala.concurrent.ExecutionContext
import scala.util.Random

class GermController @Inject()()(germDao: GermDao, cc: ControllerComponents)
                              (implicit exec:ExecutionContext)extends AbstractController(cc) with MyRow{

  def get8Banner: Action[AnyContent] = Action.async{
    val seqs = (1 to 192).map(x=>x)
    val ids = Random.shuffle(seqs).take(8)
    germDao.getByIds(ids).map{x=>
      Ok(Json.toJson(x.map(_.getJsonByT())))
    }
  }

  def getAllGerm: Action[AnyContent] = Action.async{ implicit request=>
    germDao.getAllGerm.map{x=>
      Ok(Json.toJson(x.map(_.getJsonByT())))
    }
  }

  def getById(id:Int): Action[AnyContent] = Action.async{ implicit request=>
    germDao.getById(id).map{x=>
      Ok(Json.toJson(x.getJsonByT()))
    }
  }

  def getTreeData: Action[AnyContent] = Action{ implicit request=>
    Ok(Json.toJson(TableUtils.treeData))
  }

  def searchData(content:String,types:String) = Action.async{
    germDao.getByPos(content,types).map{x=>
      Ok(Json.toJson(x.map(_.getJsonByT())))
    }
  }

}
